﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.BLL;
using Register.Command;
using Register.Model;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.SystemUserManager.ashx
{
    /// <summary>
    /// DoctorUserHandler 的摘要说明
    /// </summary>
    public class DoctorUserHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            // List<DoctorInfo> list;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string action = context.Request["action"];
            #region 添加
            if (action.Equals("addDoctorUser"))
            {
                DoctorUserInfo doctorUser = new DoctorUserInfo();

                doctorUser.DoctorUser_ID = DataIDHelper.GetDataID("DoctorUser_ID");
                doctorUser.Doctor_ID = context.Request["id"];
                doctorUser.DoctorUser_UserName = context.Request["DoctorUser_UserName"];
                doctorUser.DoctorUser_Password = MD5Provider.Hash(context.Request["DoctorUser_Password"]);
                doctorUser.DoctorUser_AddTime = DateTime.Now;
                doctorUser.DoctorUser_Recoder = "DoubleZ";  //临时赋值
                doctorUser.DoctorUser_State = "1";
                context.Response.Write(jss.Serialize(new message(DoctorUserInfoBLL.AddDoctorUserInfo(doctorUser), "医生用户添加成功！")));

            }
            #endregion
            #region 获取医生用户
            else if (action.Equals("get"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<DoctorUserInfo> list2 = new List<DoctorUserInfo>();
                list2 = DoctorUserInfoBLL.GetPagedDoctorUserInfo(row, page, " 1=1 ");

                List<DoctorUserInfoJSON> json = new List<DoctorUserInfoJSON>();
                foreach (DoctorUserInfo doc in list2)
                { //对于每一个医院对象，根据获得的城市ID获得城市名，并绑定
                    json.Add(new DoctorUserInfoJSON(doc.DoctorUser_ID, DoctorUserInfoBLL.GetDoctorNameById(doc.Doctor_ID), doc.DoctorUser_UserName, doc.DoctorUser_Password, doc.DoctorUser_AddTime.ToString(), doc.DoctorUser_Recoder));
                }

                DoctorUserInfoListJSON DoctorUserListJSON = new DoctorUserInfoListJSON();
                DoctorUserListJSON.total = DoctorUserInfoBLL.GetDoctorDoctorInfoCount(null);
                DoctorUserListJSON.rows = json;

                JavaScriptSerializer jss1 = new JavaScriptSerializer();
                context.Response.Write(jss1.Serialize(DoctorUserListJSON));
            }
            #endregion
            #region 删除医生用户
            else if (action.Equals("delete"))
            {
                String ID = context.Request["id"];
                if (DoctorUserInfoBLL.DeleteDoctorUserInfo(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            #endregion
            #region 更新医生用户
            else if (action.Equals("edit"))
            {
                DoctorUserInfo doctor = new DoctorUserInfo();
                
                doctor.DoctorUser_ID = context.Request["id"];
                doctor.DoctorUser_Password = context.Request["DoctorUser_Password"];
                doctor.DoctorUser_UserName = context.Request["DoctorUser_UserName"];
                doctor.DoctorUser_LastAmendTime = DateTime.Now;
                doctor.DoctorUser_LastAmendPerson = "DoubleZ";
              
                context.Response.Write(jss.Serialize(new message(DoctorUserInfoBLL.UpdateDoctorUserInfo(doctor), "编辑用户成功！")));

            }
            #endregion
            #region 检查医生用户是否已经存在
            else if (action.Equals("check"))
            {
                string name = context.Request["name"];
                if (DoctorUserInfoBLL.Exists(name))
                {
                    context.Response.Write("ok");
                }
                else
                {
                    context.Response.Write("no");
                }
            }

        
            #endregion
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class message2
{
    public message2(bool success, string msg)
    {
        this.msg = msg;
        this.success = success;
    }
    public bool success { get; set; }
    public string msg { get; set; }
}
class DoctorUserInfoListJSON
{

    public int total { get; set; }
    public List<DoctorUserInfoJSON> rows { get; set; }

}
class DoctorUserInfoJSON
{
    public string DoctorUser_ID
    {
        set;
        get;
    }
    public string Doctor_ID
    {
        get;
        set;
    }
    public string DoctorUser_UserName
    {
        get;
        set;
    }

    public string DoctorUser_Password
    {
        get;
        set;
    }
    public string DoctorUser_AddTime{
    get;
    set;
    }
    public string DoctorUser_Recoder	
    {
        get;
        set;
    }

 
    public DoctorUserInfoJSON(string DoctorUser_ID ,string Doctor_ID,string DoctorUser_UserName,string DoctorUser_Password,string DoctorUser_AddTime,string DoctorUser_Recoder)
    {
        this.DoctorUser_ID=DoctorUser_ID;
        this.Doctor_ID=Doctor_ID;
        this.DoctorUser_UserName=DoctorUser_UserName;
        this.DoctorUser_Password=DoctorUser_Password;
        this.DoctorUser_AddTime=DoctorUser_AddTime;
        this.DoctorUser_Recoder=DoctorUser_Recoder;
       
    }

}
